package net
{
	import Base.Net.NetSocket;
	
	import Common.GameDefine;
	import Common.GameNetEvent;
	import Common.GameNetManager;
	import Common.GameResManager;
	
	import config.EnemyConfig;
	import config.ItemConfig;
	
	import flash.utils.ByteArray;
	
	import logic.GameCreatureData;
	import logic.GameItem;
	import logic.GamePetData;
	
	import ui.PetUIHandler;
	import ui.TeamUIHandler;
	
	
	public class PlayerNetHandler
	{
		public function PlayerNetHandler()
		{
		}
		
		private static function readPetData( a:ByteArray , data:GamePetData ):void
		{
			data.id = a.readShort();
			data.photo = a.readShort();
		}
		
		private static function readCreatureData( a:ByteArray , data:GameCreatureData ):void
		{
			data.HP = a.readShort();
			data.MAXHP = a.readShort();
			data.MP = a.readShort();
			data.MAXMP = a.readShort();
			
			data.Attack = a.readShort();
			data.Defence = a.readShort();
			data.Quick = a.readShort();
			data.Charm = a.readShort();
			
			data.Earth = a.readByte();
			data.Water = a.readByte();
			data.Fire = a.readByte();
			data.Wind = a.readByte();
			
			data.ID = a.readInt();
			data.Index = a.readInt();
			
			data.Name = a.readUTFBytes( 32 );
			
			data.Role = a.readByte();
			data.Color = a.readByte();
			data.Eye = a.readByte();
			data.Mouth = a.readByte();
			
			data.Vitality = a.readShort();
			data.Strength = a.readShort();
			data.Toughness = a.readShort();
			data.Dexterity = a.readShort();
			
			data.Type = a.readShort();
			
			data.DP = a.readShort();
			data.EXP = a.readInt();
			data.NextEXP = a.readInt();
			
			data.LevelUpPoint = a.readShort();
			data.Level = a.readByte();
			data.LevelPRO = a.readByte();
		}
		
		public static function readItem( a:ByteArray , item:GameItem ):void
		{
			item.item = a.readByte();
			item.type = a.readByte();
			a.readByte();
			a.readByte();
			
			item.id = a.readInt();
			item.image = a.readInt();
			item.sell = a.readInt();
			item.num = a.readInt();
			
			item.px = a.readByte();
			item.py = a.readByte();
			item.i = a.readByte();
			a.readByte();
			
			var obj:Object = ItemConfig.instance.getString( item.id );
			item.name = obj.@n;
			item.explain = obj.@e;
			
			obj = null;
		}
		
		public static function onRecvRoleInfo( data:ByteArray ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			readCreatureData( data , mainPlayer.data );
			
			mainPlayer.MapID = data.readInt();
			mainPlayer.setPos( data.readShort() , data.readShort() );
			
			mainPlayer.stone = data.readInt();
			
			var num:int = data.readInt();
			
			for ( var i:int = 0 ; i < num ; i++ )
			{
				var item:GameItem = new GameItem();
				
				readItem( data , item );
				
				if ( item.item )
					mainPlayer.addItem( item );
				else
					mainPlayer.addEquip( item );
			}
			
			mainPlayer.updateActionID();
			
			
		}
		
		public static function SendGetPet():void
		{
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			
			netSocket.writeShort( GameNetEvent.SEND_GET_PET_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_GET_PET_ID );
			
			netSocket.writeInt( 0 );
			
			netSocket.Send();
		}
		
		public static function onRecvGetPet( data:ByteArray ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			 
			var count:int = data.readInt();
			
			for ( var i:int = 0 ; i < count ; i++ )
			{
				var gamePet:GamePet = new GamePet();
				gamePet.init();
				
				readCreatureData( data , gamePet.data );
				readPetData( data , gamePet.petData );
				
				gamePet.petData.name = EnemyConfig.instance.getString( gamePet.data.Index ).@n;
				
				GameCreatureManager.instance.addPet( gamePet );
				
				gamePet.updateActionID();
				
				mainPlayer.addPet( gamePet );
				
				if ( gamePet.Type == GamePet.GPT_ATTACK )
				{
					mainPlayer.attackPet = gamePet;
				}
			}
			
			PetUIHandler.instance.updateData();
			TeamUIHandler.instance.updateData();
			
		}
		
		
		public static function SendBattle( type:int , id:int ):void
		{
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			
			netSocket.writeShort( GameNetEvent.SEND_BATTLE_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_BATTLE_ID );
			
			netSocket.writeInt( type );
			netSocket.writeInt( id );
			
			netSocket.Send();
		}
	}
	
	
}

